<?php
/**
 * User: xuhong
 * Date: 2018/11/26
 */

namespace App\Services\Fuyou;


use App\Services\Common\HttpCurl;
use App\Services\Common\ZSign;
use App\Services\Common\ZString;
use App\Services\Common\ZXml;

class FuYouBenefit
{
    // 接口域名
    public static $api_domain = "http://180.168.100.155:9600";

    /**
     * 导入规则
     * @param $request
     * @return mixed
     * @throws \Exception
     */
    public static function importRule($request)
    {
        // 获取支付参数
        $param_sign = self::beforeRequest();
        // 接口地址
        $url_api = self::$api_domain . "/importRule.fuiou";

        // 合同编号
        $param_sign["contractSsn"] = $request["contractSsn"];
        // 规则编号
        $param_sign["ruleNo"] = $request["ruleNo"];
        // 分账通知地址
        $param_sign["notifyUrl"] = $request["notifyUrl"];

        // 分账入账户
        $param_attach["list"]["item"]["accountIn"] = $request["accountIn"];
        // 分账固定金额
        $param_attach["list"]["item"]["allocateFixedAmt"] = $request["allocateFixedAmt"];
        // 分账比例
        $param_attach["list"]["item"]["allocateScale"] = $request["allocateScale"];

        $response = self::sendRequest($url_api, $param_sign, $param_attach);

        return self::afterRequest($response);
    }


    /**
     * 分账接口
     * @param $request
     * @return mixed
     * @throws \Exception
     */
    public static function doAllocate($request)
    {
        // 获取支付参数
        $param_sign = self::beforeRequest();
        // 接口地址
        $url_api = self::$api_domain . "/doAllocate.fuiou";

        $param_sign["allocateRuleNo"] = $request["allocateRuleNo"];

        // 源交易流水号
        $param_sign["srcFasSsn"] = $request["srcFasSsn"];
        // 源交易日期
        $param_sign["srcFasDate"] = $request["srcFasDate"];

        // 发送请求
        $response = self::sendRequest($url_api, $param_sign);

        return self::afterRequest($response);
    }


    /**
     * 查询分账订单
     * @param $request
     * @return mixed
     * @throws \Exception
     */
    public static function queryAllocate($request)
    {
//         获取支付参数
//        $param_sign = self::beforeRequest();

        // 接口地址
        $url_api = self::$api_domain . "/queryAllocate.fuiou";

        // 分账起始日期
        $param_sign["startDate"] = $request["startDate"];
        // 分账截止日期
        $param_sign["endDate"] = $request["endDate"];

        // 页码
        $param_sign["pageNo"] = $request["pageNo"];
        // 分页大小
        $param_sign["pageSize"] = $request["pageSize"];

        // 源交易流水号
        $param_sign["srcFasSsn"] = $request["srcFasSsn"] ?? "";
        // 源交易日期
        $param_sign["srcFasDate"] = $request["srcFasDate"] ?? "";

        // 发送请求
        $response = self::sendRequest($url_api, $param_sign);

        return self::afterRequest($response);
    }


    /**
     * 请求前操作
     * @return mixed
     */
    public static function beforeRequest()
    {
        // 机构号
//        $param_sign["ins_cd"] = config("app.fuyou_setting.ins_cd");
        $param_sign["insCd"] = "08A9999999";

        return $param_sign;
    }

    /**
     * @param $url
     * @param $param_sign
     * @param array $param_attach
     * @return string
     * @throws \Exception
     */
    public static function sendRequest($url, $param_sign, $param_attach = [])
    {

//        $private_key = config("app.fuyou_setting.private_key");
        $private_key = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJgAzD8fEvBHQTyxUEeK963mjziMWG7nxpi+pDMdtWiakc6xVhhbaipLaHo4wVI92A2wr3ptGQ1/YsASEHm3m2wGOpT2vrb2Ln/S7lz1ShjTKaT8U6rKgCdpQNHUuLhBQlpJer2mcYEzG/nGzcyalOCgXC/6CySiJCWJmPyR45bJAgMBAAECgYBHFfBvAKBBwIEQ2jeaDbKBIFcQcgoVa81jt5xgz178WXUg/awu3emLeBKXPh2i0YtN87hM/+J8fnt3KbuMwMItCsTD72XFXLM4FgzJ4555CUCXBf5/tcKpS2xT8qV8QDr8oLKA18sQxWp8BMPrNp0epmwun/gwgxoyQrJUB5YgZQJBAOiVXHiTnc3KwvIkdOEPmlfePFnkD4zzcv2UwTlHWgCyM/L8SCAFclXmSiJfKSZZS7o0kIeJJ6xe3Mf4/HSlhdMCQQCnTow+TnlEhDTPtWa+TUgzOys83Q/VLikqKmDzkWJ7I12+WX6AbxxEHLD+THn0JGrlvzTEIZyCe0sjQy4LzQNzAkEAr2SjfVJkuGJlrNENSwPHMugmvusbRwH3/38ET7udBdVdE6poga1Z0al+0njMwVypnNwy+eLWhkhrWmpLh3OjfQJAI3BV8JS6xzKh5SVtn/3Kv19XJ0tEIUnn2lCjvLQdAixZnQpj61ydxie1rggRBQ/5vLSlvq3H8zOelNeUF1fT1QJADNo+tkHVXLY9H2kdWFoYTvuLexHAgrsnHxONOlSA5hcVLd1B3p9utOt3QeDf6x2i1lqhTH2w8gzjvsnx13tWqg==";

//         改变编码，传成GBK
//        $param_sign = ZString::array2GBK($param_sign);

        $param_api = $param_sign;

        // 排序与合并成字符串
        $sign = ZString::sortAndCombine($param_sign);

        // RSAwithMD5+base64加密后得到的sign
        $param_api["signature"] = ZSign::private_sign($sign, $private_key, "RSA2", "MD5");

        // 判断是否有附加数据
        if (!empty($param_attach)) {
            $param_api = array_merge($param_api, $param_attach);
        }


        $param_api = "<?xml version=\"1.0\" encoding=\"GBK\"?><xml>" . ZXml::arrayToXml2($param_api) . "</xml>";

        var_dump($param_api);

        // 经过两次urlencode()之后的字符串
        $param_api = "req=" . urlencode(urlencode($param_api));

        //返回的xml字符串
        $response = URLdecode(HttpCurl::doPost($url, $param_api));

        return $response;
    }


    /**
     * 返回结果
     * @param $response
     * @return mixed
     * @throws \Exception
     */
    public static function afterRequest($response)
    {
        $response = iconv('gbk', 'utf-8//TRANSLIT', $response);

        var_dump($response);

        $response = ZXml::xml2array($response);


        if ($response["respCode"] == "0000") {
            $result["status"] = "1";
            $result["msg"] = "success";
            $result["data"] = $response;
        } else {
            $result["status"] = "0";
            $result["msg"] = $response["respDesc"];
        }

        return $result;
    }
}